package com_execities3;

public class test3 {
    public static void main(String[] args) {

    //1，创建一个数组用来存储学生对象
    Student[] arr = new Student[3];

    //2，创建学生对象
    Student stu1 = new Student(1,"zhangsan",23);
    Student stu2 = new Student(2,"lisi",24);
    Student stu3 = new Student(3,"wangwu",23);

    //3，把学生对象添加到数组当中
    arr[0] = stu1;
    arr[1] = stu2;
    arr[2] = stu3;

    //4，先要找到id为2的学生索引
    int index = getIndex(arr,2);

    //5,判断索引
        if (index >= 0){
        //存在，年龄加1;
        Student stu = arr[index];
        int newAge = stu.getAge()+1;
        stu.setAge(newAge);
        System.out.println("修改成功：");
        printArr(arr);

    }else{
        //不存在，提示
        System.out.println("当前id不存在，修改失败");
    }
}
    public static int getIndex(Student[] arr,int id) {
        for (int i = 0; i < arr.length; i++) {
            //依次得到每一个学生对象
            Student stu = arr[i];
            //对stu进行一个非空判断
            if (stu != null){
                int sid = stu.getId();
                if(sid == id){
                    return i;
                }
            }
        }
        //当循环结束之后，还没有找到就是表示不存在
        return -1;
    }
    public static void printArr(Student[] arr){
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if (stu != null){
                System.out.println(stu.getId()+","+ stu.getAge()+","+stu.getName());
            }
        }
    }
}


